import torch
import torch.nn as nn
import torch.nn.functional as F

class SelfAttention(nn.Module):
    def __init__(self, hidden_size):
        super(SelfAttention, self).__init__()
        self.query = nn.Linear(hidden_size * 2, hidden_size)
        self.key = nn.Linear(hidden_size * 2, hidden_size)
        self.value = nn.Linear(hidden_size * 2, hidden_size)
        self.gamma = nn.Parameter(torch.zeros(1))

    def forward(self, hidden_states):
        batch_size, seq_len, _ = hidden_states.size()
        hidden_states = hidden_states.reshape(-1, hidden_states.size(-1))

        query = self.query(hidden_states)
        key = self.key(hidden_states)
        value = self.value(hidden_states)

        query = query.view(batch_size, seq_len, -1)
        key = key.view(batch_size, seq_len, -1)
        scores = torch.bmm(query, key.transpose(1, 2)) / (torch.sqrt(torch.tensor(query.size(-1), dtype=torch.float32).to(hidden_states.device)) / torch.sqrt(torch.tensor(2, dtype=torch.float32).to(hidden_states.device)))
        attention_weights = F.softmax(scores, dim=-1)

        value = value.view(batch_size, seq_len, -1)
        context = torch.bmm(attention_weights, value)

        hidden_states = hidden_states.view(batch_size, seq_len, -1)
        hidden_states = hidden_states[:, :, :hidden_states.size(2) // 2]

        output = self.gamma * context + hidden_states

        return output


class BiSA_GRU(nn.Module):
    def __init__(self, input_size, hidden_size, batch_first=True):
        super(BiSA_GRU, self).__init__()
        self.hidden_size = hidden_size
        self.gru = nn.GRU(input_size, hidden_size, batch_first=batch_first, bidirectional=True)
        self.self_attention = SelfAttention(hidden_size)

    def forward(self, x, h0=None):
        out, h_n = self.gru(x, h0)
        out = self.self_attention(out)
        return out

# 示例调用函数
def example_call():
    batch_size = 32
    seq_len = 10
    input_size = 64
    hidden_size = 256

    bisa_gru_layer = BiSA_GRU(input_size, hidden_size)
    inputs = torch.randn(batch_size, seq_len, input_size)
    # 使用全零的初始隐藏状态调用模型的前向传播函数
    outputs = bisa_gru_layer(inputs)

    print(f"Input shape: {inputs.shape}")
    print(f"Output shape: {outputs.shape}")

# 运行示例调用函数
# example_call()
